package storage.tx.recovery;

import server.starDB;
import storage.buffer.*;
import storage.file.Block;
import storage.log.BasicLogRecord;

class SetIntRecord implements LogRecord {
   private int txnum, offset, val;
   private Block blk;
   
   
   public SetIntRecord(int txnum, Block blk, int offset, int val) {
      this.txnum = txnum;
      this.blk = blk;
      this.offset = offset;
      this.val = val;
   }
   
  
   public SetIntRecord(BasicLogRecord rec) {
      txnum = rec.nextInt();
      String filename = rec.nextString();
      int blknum = rec.nextInt();
      blk = new Block(filename, blknum);
      offset = rec.nextInt();
      val = rec.nextInt();
   }
   
   
   public int writeToLog() {
      Object[] rec = new Object[] {SETINT, txnum, blk.getFileName(), 
         blk.getBlkNum(), offset, val};
      return logMgr.append(rec);
   }
   
   public int op() {
      return SETINT;
   }
   
   public int txNumber() {
      return txnum;
   }
   
   public String toString() {
      return "<SETINT " + txnum + " " + blk + " " + offset + " " + val + ">";
   }
   
   
   public void undo(int txnum) {
      BufferMgr buffMgr = starDB.bufferMgr();
      Buffer buff = buffMgr.pin(blk);
      buff.setInt(offset, val, txnum, -1);
      buffMgr.unpin(buff);
   }
}
